package com.chezai.client.utils;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * 类转换初始化
 */
public class TUtil {
    public static <T> T getT(Object o, int i) {
        try {
            ParameterizedType parameterizedType = (ParameterizedType) o.getClass().getGenericSuperclass();
            Type type = parameterizedType.getActualTypeArguments()[i];
            return (T) type;
        } catch (ClassCastException e) {
        }
        return null;
    }

    public static <T> T getType(Object o, int i) {
        try {
            return ((Class<T>) ((ParameterizedType) (o.getClass()
                    .getGenericSuperclass())).getActualTypeArguments()[i])
                    .newInstance();
        } catch (InstantiationException e) {
        } catch (IllegalAccessException e) {
        } catch (ClassCastException e) {
        }
        return null;
    }

    public static Class<?> forName(String className) {
        try {
            return Class.forName(className);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取类属性的set方法
     *
     * @param objectClass
     * @param fieldName     属性名
     * @param parameterType 属性类型
     * @return
     */
    public static Method getSetMethod(Class objectClass, String fieldName, Class parameterType) {
        try {
            StringBuffer sb = new StringBuffer();
            sb.append("set");
            sb.append(fieldName.substring(0, 1).toUpperCase());
            sb.append(fieldName.substring(1));
            Method method = objectClass.getMethod(sb.toString(), parameterType);
            return method;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
